package com.miniportal.base.exception;

import cn.hutool.http.HttpStatus;
import com.miniportal.base.pojo.CommonResponse;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * TODO
 *
 * @author zhouje
 * @since 2022/5/30 23:20
 */
public class GlobalException {

    @ExceptionHandler(BindException.class)
    public CommonResponse handeBindException(BindException  e) {
        BindingResult bindingResult = e.getBindingResult();
        FieldError fieldError = bindingResult.getFieldError();
        assert fieldError != null;
        String defaultMessage = fieldError.getDefaultMessage();
        // 输出
        return CommonResponse.builder().code(HttpStatus.HTTP_BAD_REQUEST).msg(defaultMessage).build();
    }

}
